<?php

namespace Model;

use Lib;

class EquipModel
{

    private $db = null;
    private $table = 'tbequip';
    public function __construct()
    {
        $this->db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
    }

    /**
     * 根据种族Id以及版本Id获取种族信息
     * @param $raceId
     * @param $versionId
     * @return array
     */
    public function getEquipInfo($equipId,$where)
    {

        $where['equipId'] = $equipId;
        $race = $this->db->table($this->table)->where($where)->find();
        return $race;
    }

    /**
     * 根据版本Id获取对应的版本种族
     * @param $versionId
     * @return array
     */
    public function getAllEquipBase($where)
    {
        $where['type'] = 1;
        $race = $this->db->table($this->table)->where($where)->order("equipId","asc")->select();
        return $race;
    }

    /**
     * 根据版本Id获取对应的版本种族
     * @param $versionId
     * @return array
     */
    public function getAllEquip($where)
    {
        $where['type'] = 2;
        $race = $this->db->table($this->table)->where($where)->order("equipId","asc")->select();
        return $race;
    }
    //检查TFTID是否已经存在了
    public function checkTFTID($equipId,$where,$TFTID)
    {
        $where['TFTID'] = $TFTID;
        $where['equipId'] = ['!=',$equipId];

        $res = $this->db->table($this->table)->where($where)->find();
        return $res;
    }

    /**
     * 修改种族信息
     * @param $versionId
     * @param $raceId
     * @param $data
     */
    public function updateEquip($equipId,$where,$data)
    {

        $where['equipId'] = $equipId;
        return $this->db->table($this->table)->where($where)->update($data);
    }


    public function insertRace($versionId,$seasonId,$data)
    {
        $arr_keys = array_keys($data[0]);
        array_push($arr_keys,'versionId');
        array_push($arr_keys,'seasonId');
        $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') values';
        foreach ($data as &$time){
            $time['versionId'] = $versionId;
            $time['seasonId'] = $seasonId;

            $arr_values = array_values($time);
            $sql.= " ('" . implode("','" ,$arr_values) . "'),";
        }
        $sql = substr($sql ,0 ,-1);


        $result = $this->db->table($this->table)->exec($sql);


        var_dump($result); exit();
    }


    public function insertRaceOne($versionId,$seasonId,$data)
    {
        $arr_keys = array_keys($data);
        array_push($arr_keys,'versionId');
        array_push($arr_keys,'seasonId');
        $sql = 'INSERT INTO '.$this->table .' (' . implode(',' ,$arr_keys) . ') value';
        $data['versionId'] = $versionId;
        $data['seasonId'] = $seasonId;
        $arr_values = array_values($data);
        $sql.= " ('" . implode("','" ,$arr_values) . "'),";
        $sql = substr($sql ,0 ,-1);
        $result = $this->db->table($this->table)->exec($sql);
        return $result;
    }

    //根据基础道具获取对应的合成道具
    public function getEquipByBase($equipId,$where)
    {
        //formula

        $sql = 'select * from tbequip where find_in_set('.$equipId.',formula) and versionId = '.$where['versionId'].' AND seasonId= '.$where['seasonId'].' and isDelete=0 ';
        if (isset($where['name']) && !empty(isset($where['name']))) {
            $sql .= ' and name like \'%'.$where['name'].'%\'';
        }
//        echo $sql;die;exit();

        return $this->db->table($this->table)->query($sql);
    }
}